package com.leenmvc.core.security.service.core;

import com.leenmvc.core.annotation.TransactionMulti;
import com.leenmvc.core.base.BaseService;
import com.leenmvc.core.dao.communal.Wrapper;
import com.leenmvc.core.exception.BusinessException;
import com.leenmvc.core.security.dao.core.SysLanguageDao;
import com.leenmvc.core.security.entity.core.SysLanguage;
import com.leenmvc.core.security.entity.core.SysTip;
import com.leenmvc.core.utils.http.HttpContextUtils;
import com.leenmvc.core.base.result.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysTipService extends BaseService<SysTip> {

    @Autowired
    private SysLanguageDao sysLanguageDao;

    @Override
    @TransactionMulti
    public <T> boolean insert(T t) {

        if(!super.insert(t)) {
            throw new BusinessException("INSERT_FAIL", SysTip.class);
        }
        SysTip sysTip = (SysTip)t;
        // 语言表新增一条记录
        SysLanguage language = new SysLanguage();
        language.setTableName("sys_tip");
        language.setTableId(sysTip.getId());
        language.setLanguage("zh-CN");
        language.setFieldName("identification");
        language.setFieldValue(sysTip.getTip());
        if(!sysLanguageDao.insert(language)) {
            throw new BusinessException("INSERT_FAIL", SysLanguage.class);
        }
        return true;
    }

    /**
     * 重写获取单条记录方法
     *
     * @param id
     * @param <T>
     * @return
     */
    @Override
    public <T> T get(Object id) {
        return getBySqlKey("getById", "id", id, "language", HttpContextUtils.getLanguage());
    }

    /**
     * 重写更新方法
     *
     * @param t
     * @param <T>
     * @return
     */
    @Override
    public <T> boolean update(T t) {
        SysTip sysTip = (SysTip)t;
        // 语言表新增一条记录
        Wrapper wrapper = getUpdateWrapper(SysLanguage.class);
        wrapper.addSet("fieldValue", sysTip.getTip())
                .eq("language", HttpContextUtils.getLanguage())
                .eq("tableId", sysTip.getId())
                .eq("tableName", "sys_tip");
        if(sysLanguageDao.updateByWrapper(wrapper) < 0) {
            throw new BusinessException("INSERT_FAIL！！", SysLanguage.class);
        }

        if(!super.update(t)) {
            throw new BusinessException("UPDATE_FAIL", SysTip.class);
        }
        return true;
    }

    /**
     * 重写删除方法
     *
     * @param ids
     * @return
     */
    @Override
    @TransactionMulti
    public int deleteByIds(Object[] ids) {
        Wrapper wrapper = getDeleteWrapper(SysLanguage.class);
        wrapper.in("tableId", ids);
        if(delByWrapper(wrapper) <= 0) {
            throw new BusinessException("DELETE_FAIL", SysTip.class);
        }
        return super.deleteByIds(ids);
    }

    /**
     * 重写分页方法
     *
     * @param mybatisKey
     * @param params
     * @param <T>
     * @return
     */
    @Override
    public <T> PageData<T> getPage(String mybatisKey, Map<String, Object> params) {
        params.put("language", HttpContextUtils.getLanguage());
        return super.getPage(mybatisKey, params);
    }
}
